package com.may.wlrj.services;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.context.request.async.DeferredResult;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.may.wlrj.dao.RightDAO;
import com.may.wlrj.entity.RightEntity;
import com.may.wlrj.entity.RightRejectEntity;
import com.may.wlrj.entity.UserEntity;
import com.may.wlrj.entity.UserLogEntity;
import com.may.wlrj.entity.UserRightAuthEntity;
import com.may.wlrj.util.XinLongUtil;

@Service
@Transactional
public class RightService {
	private Logger log = Logger.getLogger(RightService.class);
	private RightDAO rightDao;
	@Autowired
	public void setRigjtDAO(RightDAO rightDao) {
		this.rightDao = rightDao;
	}
	
	public String InitTree() throws JsonProcessingException{ 
		ObjectMapper mapper = new ObjectMapper();
		ArrayNode rootnode = mapper.createArrayNode();  
		//获取根节点
		try{
			List<RightEntity> rlist = rightDao.findrightByPid("");
			if(rlist != null){
				for(RightEntity right : rlist){
					ObjectNode jnode = mapper.createObjectNode();
					jnode.put("id", right.getId());
					jnode.put("title", right.getText());
					jnode.put("spread", true);
					jnode.put("open", true);
					jnode.put("sort", right.getSort());
					jnode.put("reject", getRejectString(right.getId()));
					jnode.put("needcheck", right.getNeedcheck());
					jnode.put("authtype", right.getRight_type());
					jnode.put("isleaf", right.getLeaf());
					jnode.put("createtime", right.getCreatetime());
					jnode.put("updatetime", right.getUpdatetime());
					ArrayNode subNode = findSubNodes(right.getId());
					if(subNode.size() > 0){
						//jnode.put("iconCls", "icon-institution");
						jnode.put("children", subNode);
					}else{
						//jnode.put("iconCls", "icon-finance");
					}
					rootnode.add(jnode);
				}
			}else{
				XinLongUtil.xinLongUtil.printlog(log,"-------------->>获取功能列表为空....");
			}
			
		}catch(Exception e){
			log.error("[Error]================读取功能信息异常!");
			return mapper.writeValueAsString(rootnode);
		}  
          
		
		return mapper.writeValueAsString(rootnode);
	}
	
	private ArrayNode findSubNodes(String parentid) throws JsonProcessingException{
		List<RightEntity> rlist = null;
		ObjectMapper mapper = new ObjectMapper();
		ArrayNode marksNode = mapper.createArrayNode();
		ObjectNode tnode = null;
		try{
			rlist = rightDao.findrightByPid(parentid);
			for(RightEntity org : rlist){
				tnode = mapper.createObjectNode();
				tnode.put("name", org.getName());
				tnode.put("id", org.getId());
				tnode.put("title", org.getText());
				tnode.put("sort", org.getSort());
				tnode.put("open", true);
				tnode.put("reject", getRejectString(org.getId()));
				tnode.put("needcheck", org.getNeedcheck());
				tnode.put("authtype", org.getRight_type());
				tnode.put("isleaf", org.getLeaf());
				tnode.put("createtime", org.getCreatetime());
				tnode.put("updatetime", org.getUpdatetime());
				ArrayNode subNode = findSubNodes(org.getId());
				if(subNode.size() > 0){
					//tnode.put("iconCls", "icon-institution");
					tnode.put("children", subNode);
				}else{
					//tnode.put("iconCls", "icon-finance");
				}
				
				marksNode.add(tnode);
				
			}
		}catch(EmptyResultDataAccessException er){
			XinLongUtil.xinLongUtil.printlog(log,er.getMessage());
			return marksNode;
		}
		
		return marksNode;
	}
	
	private String getRejectString(String rightid){
		List<RightRejectEntity> rejects = rightDao.findRejectdescByid(rightid);
		String restr = "";
		if(rejects != null){
			for(int i=0;i<rejects.size();i++){
				RightRejectEntity rej = rejects.get(i);
				restr += rej.getRejectname();
				if(i != rejects.size() -1){
					restr += "|";
				}
			}
		}
		return restr;
	}
	
	public String RejectTree(String jsonstr) throws JsonProcessingException{ 
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		ObjectMapper mapper = new ObjectMapper();
		ArrayNode rootnode = mapper.createArrayNode();  
		String rightid ="";
		ObjectNode res = mapper.createObjectNode();
		com.fasterxml.jackson.databind.JsonNode rootNode;
    	try{
    		rootNode = mapper.readTree(jsonstr);
    		rightid =rootNode.get("rightid").asText();
    		
    	}catch(Exception e){
    		res.put("status", "0");
			res.put("desc", "上传数据解析错误!");
			return mapper.writeValueAsString(res);
    	}
    	List<RightRejectEntity> rejects = rightDao.findRejectByid(rightid);
    	XinLongUtil.xinLongUtil.printlog(log,"互斥功能项数量:" + rejects.size());
    	try{
			List<RightEntity> rlist = rightDao.findrightByPid("");
			if(rlist != null){
				for(RightEntity right : rlist){
					ObjectNode jnode = mapper.createObjectNode();
					jnode.put("id", right.getId());
					jnode.put("name", right.getName());
					jnode.put("text", right.getText());
					jnode.put("rtype", right.getRight_type());
					if(right.getId().equals(rightid)){
						continue;
					}
					if(rejects != null){
						for(RightRejectEntity reject : rejects){
							if(reject.getReject_right_id().equals(right.getId())){
								jnode.put("checked", true);
							}
						}
					}
					ArrayNode subNode = findSubRejectNodes(right.getId(),rightid,rejects);
					if(subNode.size() > 0){
						//jnode.put("iconCls", "icon-institution");
						jnode.put("children", subNode);
					}else{
						//jnode.put("iconCls", "icon-finance");
					}
					rootnode.add(jnode);
				}
			}
			
		}catch(Exception e){
			log.error("[Error]================读取功能信息异常!");
			return mapper.writeValueAsString(rootnode);
		}  
		return mapper.writeValueAsString(rootnode);
	}
	
	private ArrayNode findSubRejectNodes(String parentid,String rejectid,List<RightRejectEntity> rejects) throws JsonProcessingException{
		List<RightEntity> rlist = null;
		ObjectMapper mapper = new ObjectMapper();
		ArrayNode marksNode = mapper.createArrayNode();
		ObjectNode tnode = null;
		try{
			rlist = rightDao.findrightByPid(parentid);
			for(RightEntity org : rlist){
				if(org.getId().equals(rejectid)){
					continue;
				}
				tnode = mapper.createObjectNode();
				tnode.put("name", org.getName());
				tnode.put("id", org.getId());
				tnode.put("rtype", org.getRight_type());
				tnode.put("text", org.getText());
				if(rejects != null){
					for(RightRejectEntity reject : rejects){
						if(reject.getReject_right_id().equals(org.getId())){
							tnode.put("checked", true);
						}
					}
				}
				ArrayNode subNode = findSubRejectNodes(org.getId(),rejectid,rejects);
				if(subNode.size() > 0){
					//tnode.put("iconCls", "icon-institution");
					tnode.put("children", subNode);
				}else{
					//tnode.put("iconCls", "icon-finance");
				}
				
				marksNode.add(tnode);
				
			}
		}catch(EmptyResultDataAccessException er){
			return marksNode;
		}
		
		return marksNode;
	}
	
	public void _RejectSave(DeferredResult<String> deferredResult,String jsonstr, HttpServletRequest request, HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
		String remoteaddr = XinLongUtil.getRemoteAddr(request);
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode res = mapper.createObjectNode();
    	String rightid ="";
    	String ckname ="";
		com.fasterxml.jackson.databind.JsonNode rootNode;
		com.fasterxml.jackson.databind.JsonNode childrenNode = null;
    	try{
    		rootNode = mapper.readTree(jsonstr);
    		rightid =rootNode.get("rightid").asText();
    		ckname =rootNode.get("ckname").asText();
    		childrenNode = rootNode.path("children"); 
    		
    	}catch(Exception e){
    		res.put("status", "0");
			res.put("desc", "上传数据解析错误!");
			deferredResult.setResult(mapper.writeValueAsString(res));
    		return;
    	}
    	//权限验证
    	/*res = XinLongUtil.OptAuth("",jsonstr, sentity);
		if(!res.get("status").asText().equals("1210")){
    		return mapper.writeValueAsString(res);
		}*/
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		UserLogEntity ulog = new UserLogEntity();
		ulog.setId(UUID.randomUUID().toString().replace("-", ""));
		ulog.setRemote_addr(remoteaddr);
		ulog.setCreatetime(dateFormat.format(date));
		ulog.setArgs_json(jsonstr);
		ulog.setRname("系统管理/功能管理/保存互斥");
		ulog.setCreateusername(sentity.getUsername());
		ulog.setUserid(sentity.getId());
		ulog.setMethod("RejectSave");
		ulog.setOrgid(sentity.getOrg_id());
		ulog.setCreate_check_userid(ckname);
		//删除老的互斥项
		XinLongUtil.xinLongUtil.printlog(log,"---->>删除老的互斥项");
		if(rightDao.deletRejectByid(rightid) == 0){
			res.put("status", "0");
			res.put("desc", "保存失败!");
			deferredResult.setResult(mapper.writeValueAsString(res));
    		return;
		}
		//更新功能项
		Iterator<com.fasterxml.jackson.databind.JsonNode> it = childrenNode.iterator();
		RightRejectEntity rej = null;
    	while(it.hasNext()){
    		com.fasterxml.jackson.databind.JsonNode jnode = it.next();
    		String reject_rightid = jnode.get("rightid").asText();
    		XinLongUtil.xinLongUtil.printlog(log,"------->>rejectid:" + reject_rightid);
    		rej = new RightRejectEntity();
    		rej.setId(UUID.randomUUID().toString().replace("-", ""));
    		rej.setCreatetime(dateFormat.format(date));
    		rej.setCreatename(sentity.getUsername());
    		rej.setReject_right_id(reject_rightid);
    		rej.setRight_id(rightid);
    		rej.setCheckid(res.get("ckuserid").asText());
    		if(rightDao.savereject(rej) == 0){
    			res.put("status", "0");
    			res.put("desc", "保存互斥失败!");
    			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
    			ulog.setProceed("操作失败");
    			XinLongUtil.saveUserLog(ulog);
    			deferredResult.setResult(mapper.writeValueAsString(res));
        		return;
    		}
    	}
		res.put("status", "1");
		res.put("desc", "操作成功!");
		ulog.setProceed("操作成功");
		XinLongUtil.saveUserLog(ulog);
		deferredResult.setResult(mapper.writeValueAsString(res));
		return; 
	}

	
	public void _editRight(DeferredResult<String> deferredResult,String jsonstr, HttpServletRequest request, HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
		String remoteaddr = XinLongUtil.getRemoteAddr(request);
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode res = mapper.createObjectNode();
    	String rightid ="";
    	String ckname ="";
		String rgtext="";
		int sort = 0;
		int needcheck = 0;
		com.fasterxml.jackson.databind.JsonNode rootNode;
    	try{
    		rootNode = mapper.readTree(jsonstr);
    		rightid =rootNode.get("rightid").asText();
    		ckname =rootNode.get("ckname").asText();
    		rgtext =rootNode.get("rgtext").asText();
    		sort=rootNode.get("sort").asInt();
    		needcheck=rootNode.get("needcheck").asInt();
    		
    	}catch(Exception e){
    		res.put("status", "0");
			res.put("desc", "上传数据解析错误!");
			log.error(e.getMessage());
			deferredResult.setResult(mapper.writeValueAsString(res));
    		return;
    	}
    	//权限验证
    	/*res = XinLongUtil.OptAuth("",jsonstr, sentity);
		if(!res.get("status").asText().equals("1210")){
			deferredResult.setResult(mapper.writeValueAsString(res));
    		return;
		}*/
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		UserLogEntity ulog = new UserLogEntity();
		ulog.setId(UUID.randomUUID().toString().replace("-", ""));
		ulog.setRemote_addr(remoteaddr);
		ulog.setCreatetime(dateFormat.format(date));
		ulog.setArgs_json(jsonstr);
		ulog.setRname("系统管理/功能管理/保存");
		ulog.setCreateusername(sentity.getUsername());
		ulog.setUserid(sentity.getId());
		ulog.setMethod("editRight");
		ulog.setOrgid(sentity.getOrg_id());
		ulog.setCreate_check_userid(ckname);
		//更新功能项
		RightEntity rgentity = rightDao.findByRightId(rightid);
		if(rgentity == null){
			res.put("status", "0");
			res.put("desc", "获取功能信息失败!");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			deferredResult.setResult(mapper.writeValueAsString(res));
    		return;
		}
		
		rgentity.setUpdatename(sentity.getUsername());
		rgentity.setUpdatetime(dateFormat.format(date));
		rgentity.setSort(sort);
		rgentity.setText(rgtext);
		rgentity.setNeedcheck(needcheck);
		if(rightDao.updateRight(rgentity) != 1){
			res.put("status", "0");
			res.put("desc", "更新功能信息失败!");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			deferredResult.setResult(mapper.writeValueAsString(res));
    		return;
		}
		res.put("status", "1");
		res.put("desc", "操作成功!");
		ulog.setProceed("操作成功");
		XinLongUtil.saveUserLog(ulog);
		deferredResult.setResult(mapper.writeValueAsString(res));
		return;
	}
	
	public String getRightAuthList(String jsonstr,HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode resjson = mapper.createObjectNode();
    	int pageNumber = 0;
		int pageSize = 0;
		String start = "";
		String end = "";
		com.fasterxml.jackson.databind.JsonNode rootNode;
		try {
			rootNode = mapper.readTree(jsonstr);
			pageNumber =rootNode.get("pageNumber").asInt();
			pageSize =rootNode.get("pageSize").asInt();
			start =rootNode.get("start").asText();
			end =rootNode.get("end").asText();			
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			Date dd = sdf2.parse(end);
			Calendar c = Calendar.getInstance();  
            c.setTime(dd);  
            c.add(Calendar.DAY_OF_MONTH, 1);              
            dd = c.getTime();
            end = sdf2.format(dd);
		} catch (Exception e1) {
			resjson.put("status", "2");
			resjson.put("desc", "解析上传数据异常!");
			log.error(e1.getMessage());
			return mapper.writeValueAsString(resjson);
		}
		
		if(start.compareTo(end) == 1){
			resjson.put("status", "2");
			resjson.put("desc", "起始日期小于结束日期!");
			return mapper.writeValueAsString(resjson);
		}
		
		List<UserRightAuthEntity> cplist = rightDao.findRightAuthAll(pageNumber, pageSize, start, end,sentity.getOrg_id());
		if(cplist != null){
			ArrayNode rootnode = mapper.createArrayNode();
			ObjectNode tjson = null;
			Date date = new Date();
			SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for(UserRightAuthEntity cap : cplist){
				//验证授权是否过期
				if(cap.getStatus() == 0 && cap.getEnd_time().compareTo(dateFormat.format(date)) < 1){
	    			//已过期,更新授权状态
					rightDao.updateStatusOnly(1, cap.getId());
					cap.setStatus(1);
				}
				tjson = mapper.createObjectNode();
				tjson.put("id", cap.getId());
    			tjson.put("createtime", cap.getCreatetime());
    			tjson.put("start_time", cap.getStart_time());
    			tjson.put("end_time", cap.getEnd_time());
    			tjson.put("status", cap.getStatus());
    			tjson.put("to_username", cap.getTo_username());
    			tjson.put("from_username", cap.getFrom_username());
    			rootnode.add(tjson);
			}
			resjson.put("status", "1");
			resjson.put("desc", "获取成功!");
			resjson.put("total", rightDao.getauthCount(start, end,sentity.getOrg_id()));
			resjson.put("pageSize", pageSize);
			resjson.put("datas", rootnode);
			return mapper.writeValueAsString(resjson);
		}else{
			resjson.put("status", "2");
			resjson.put("desc", "获取数据异常!");
			return mapper.writeValueAsString(resjson);
		}
    }
	
	public void _rightAuthCancel(DeferredResult<String> deferredResult,String jsonstr, HttpServletRequest request, HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
		String remoteaddr = XinLongUtil.getRemoteAddr(request);
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode res = mapper.createObjectNode();
    	String id ="";
    	String ckname ="";
		com.fasterxml.jackson.databind.JsonNode rootNode;
    	try{
    		rootNode = mapper.readTree(jsonstr);
    		id =rootNode.get("id").asText();
    		ckname =rootNode.get("ckname").asText();
    	}catch(Exception e){
    		res.put("status", "0");
			res.put("desc", "上传数据解析错误!");
			log.error(e.getMessage());
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
    	}
    	//权限验证
    	/*res = XinLongUtil.OptAuth(id,jsonstr, sentity);
		if(!res.get("status").asText().equals("1210")){
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}*/

		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		UserLogEntity ulog = new UserLogEntity();
		ulog.setId(UUID.randomUUID().toString().replace("-", ""));
		ulog.setRemote_addr(remoteaddr);
		ulog.setCreatetime(dateFormat.format(date));
		ulog.setArgs_json(jsonstr);
		ulog.setRname("系统管理/授权管理/撤消授权");
		ulog.setCreateusername(sentity.getUsername());
		ulog.setUserid(sentity.getId());
		ulog.setMethod("rightAuthCancel");
		ulog.setOrgid(sentity.getOrg_id());
		ulog.setCreate_check_userid(ckname);
		//更新功能项
		
		
		if(rightDao.updateStatus(id,ckname,dateFormat.format(date),sentity.getUsername()) != 1){
			res.put("status", "0");
			res.put("desc", "更新授权信息失败!");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}
		res.put("status", "1");
		res.put("desc", "操作成功!");
		ulog.setProceed("操作成功");
		XinLongUtil.saveUserLog(ulog);
		deferredResult.setResult(mapper.writeValueAsString(res));
		return;
	}
	
	public void _rightAuthAdd(DeferredResult<String> deferredResult,String jsonstr, HttpServletRequest request, HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
		String remoteaddr = XinLongUtil.getRemoteAddr(request);
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode res = mapper.createObjectNode();
    	String id ="";
    	String stime ="";
    	String etime ="";
    	String ckname ="";
		com.fasterxml.jackson.databind.JsonNode rootNode;
    	try{
    		rootNode = mapper.readTree(jsonstr);
    		id =rootNode.get("id").asText();
    		stime =rootNode.get("stime").asText();
    		etime =rootNode.get("etime").asText();
    		ckname =rootNode.get("ckname").asText();
    		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		Date start = null;
    		Date end = null;
    		
        	try {
        		start = dateFormat.parse(stime);
        		end = dateFormat.parse(etime);
        		if(end.before(start)) {
        			res.put("status", "2");
        			res.put("desc", "终止时间应不早于生效时间!");
        			deferredResult.setResult(mapper.writeValueAsString(res));
        			return;
        		}
        		if(end.before(new Date())) {
        			res.put("status", "2");
        			res.put("desc", "终止时间应不早于当前时间!");
        			deferredResult.setResult(mapper.writeValueAsString(res));
        			return;
        		}
        	}catch(Exception e) {
        		res.put("status", "2");
        		res.put("desc", "任务时间设置异常!");
    			deferredResult.setResult(mapper.writeValueAsString(res));
    			return;
        	}
    	}catch(Exception e){
    		res.put("status", "0");
			res.put("desc", "上传数据解析错误!");
			log.error(e.getMessage());
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
    	}
    	//权限验证
    	/*res = XinLongUtil.OptAuth(id,jsonstr, sentity);
		if(!res.get("status").asText().equals("1210")){
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}*/
    	
		//添加授权列表
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		UserLogEntity ulog = new UserLogEntity();
		ulog.setId(UUID.randomUUID().toString().replace("-", ""));
		ulog.setRemote_addr(remoteaddr);
		ulog.setCreatetime(dateFormat.format(date));
		ulog.setArgs_json(jsonstr);
		ulog.setRname("系统管理/授权管理/新增或修改");
		ulog.setCreateusername(sentity.getUsername());
		ulog.setUserid(sentity.getId());
		ulog.setMethod("rightAuthCancel");
		ulog.setOrgid(sentity.getOrg_id());
		ulog.setCreate_check_userid(ckname);
		UserRightAuthEntity ura = new UserRightAuthEntity();
		ura.setId(UUID.randomUUID().toString().replace("-", ""));
		ura.setCreate_check_userid(ckname);
		ura.setCreatetime(dateFormat.format(date));
		ura.setStart_time(stime);
		ura.setEnd_time(etime);
		ura.setCreateusername(sentity.getUsername());
		ura.setFrom_userid(sentity.getId());
		ura.setTo_userid(id);
		ura.setStatus(0);
		XinLongUtil.xinLongUtil.printlog(log,"添加授权信息:" + mapper.writeValueAsString(ura));
		if(rightDao.saveurauth(ura) != 1){
			res.put("status", "0");
			res.put("desc", "添加授权信息失败!");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}
		res.put("status", "1");
		res.put("desc", "操作成功!");
		ulog.setProceed("操作成功");
		XinLongUtil.saveUserLog(ulog);
		deferredResult.setResult(mapper.writeValueAsString(res));
		return;
	}
	
	public void _rightAuthEdit(DeferredResult<String> deferredResult,String jsonstr, HttpServletRequest request, HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
		String remoteaddr = XinLongUtil.getRemoteAddr(request);
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode res = mapper.createObjectNode();
    	String id ="";
    	String stime ="";
    	String etime ="";
    	String authid ="";
    	String ckname ="";
		com.fasterxml.jackson.databind.JsonNode rootNode;
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	try{
    		rootNode = mapper.readTree(jsonstr);
    		id =rootNode.get("id").asText();
    		authid =rootNode.get("authid").asText();
    		ckname =rootNode.get("ckname").asText();
    		stime =rootNode.get("stime").asText();
    		etime =rootNode.get("etime").asText();
    		Date start = null;
    		Date end = null;
    		
        	try {
        		start = dateFormat.parse(stime);
        		end = dateFormat.parse(etime);
        		if(end.before(start)) {
        			res.put("status", "2");
        			res.put("desc", "终止时间应不早于生效时间!");
        			deferredResult.setResult(mapper.writeValueAsString(res));
        			return;
        		}
        		if(end.before(new Date())) {
        			res.put("status", "2");
        			res.put("desc", "终止时间应不早于当前时间!");
        			deferredResult.setResult(mapper.writeValueAsString(res));
        			return;
        		}
        	}catch(Exception e) {
        		res.put("status", "2");
        		res.put("desc", "任务时间设置异常!");
    			deferredResult.setResult(mapper.writeValueAsString(res));
    			return;
        	}
    	}catch(Exception e){
    		res.put("status", "0");
			res.put("desc", "上传数据解析错误!");
			log.error(e.getMessage());
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
    	}
    	//权限验证
    	/*res = XinLongUtil.OptAuth(id,jsonstr, sentity);
		if(!res.get("status").asText().equals("1210")){
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}*/
		UserLogEntity ulog = new UserLogEntity();
		ulog.setId(UUID.randomUUID().toString().replace("-", ""));
		ulog.setRemote_addr(remoteaddr);
		ulog.setCreatetime(dateFormat.format(date));
		ulog.setArgs_json(jsonstr);
		ulog.setRname("系统管理/授权管理/新增或修改");
		ulog.setCreateusername(sentity.getUsername());
		ulog.setUserid(sentity.getId());
		ulog.setMethod("rightAuthEdit");
		ulog.setOrgid(sentity.getOrg_id());
		ulog.setCreate_check_userid(ckname);
		//更新授权列表
		
		UserRightAuthEntity ura = rightDao.findRightAuthByid(authid);
		if(ura != null){
			ura.setStart_time(stime);
			ura.setEnd_time(etime);
			ura.setUpdateusername(sentity.getUsername());
			ura.setFrom_userid(sentity.getId());
			ura.setTo_userid(id);
			ura.setStatus(0);
			ura.setUpdate_check_userid(ckname);
			ura.setUpdatetime(dateFormat.format(date));
			ura.setUpdateusername(sentity.getUsername());
		}else{
			res.put("status", "0");
			res.put("desc", "查找授权信息失败!");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}
		XinLongUtil.xinLongUtil.printlog(log,"更新授权信息:" + mapper.writeValueAsString(ura));
		
		if(rightDao.updateRightAuth(ura) != 1){
			res.put("status", "0");
			res.put("desc", "更新授权信息失败!");
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			deferredResult.setResult(mapper.writeValueAsString(res));
			return;
		}
		res.put("status", "1");
		res.put("desc", "操作成功!");
		ulog.setDescriptor("修改授权");
		ulog.setProceed("操作成功");
		XinLongUtil.saveUserLog(ulog);
		deferredResult.setResult(mapper.writeValueAsString(res));
		return;  
	}
	
	public void _optcheck(DeferredResult<String> deferredResult,String jsonstr, HttpServletRequest request, HttpSession session) throws JsonProcessingException{
		XinLongUtil.xinLongUtil.printlog(log,jsonstr);
		UserEntity sentity = (UserEntity)session.getAttribute("USER_SESSION");
    	ObjectMapper mapper = new ObjectMapper(); 
    	ObjectNode res = mapper.createObjectNode();
    	
    	//权限判断
    	/*res = XinLongUtil.OptAuth("",jsonstr, sentity);
		if(!res.get("status").asText().equals("1210")){
    		deferredResult.setResult(mapper.writeValueAsString(res));
			return;  
		}*/		
		res.put("status", "1");
		//res.put("ckuserid", res.get("ckuserid"));
		res.put("desc", "操作成功!");
		deferredResult.setResult(mapper.writeValueAsString(res));
		return;  
	}
}